package com.shoppingCart.controller;


import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.lly835.bestpay.rest.type.Get;
import com.shoppingCart.converter.UserInfoToCustomer;
import com.shoppingCart.dataobject.Customer;
import com.shoppingCart.dataobject.Parking;
import com.shoppingCart.enums.ResultEnum;
import com.shoppingCart.exception.ParkingException;
import com.shoppingCart.service.CustomerService;
import com.shoppingCart.service.ParkingService;
import com.shoppingCart.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * create By lihao
 */
@Slf4j
@RestController
@RequestMapping("/wechat/user")
public class CustomerController {

    @Autowired
    private WxMaService wxMaService;

    @Autowired
    private ParkingService parkingService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CustomerService customerService;

    @GetMapping("login")
    public String login(String code) {
        if (StringUtils.isBlank(code)) {
            log.error("login Code为空！");
            throw new ParkingException(ResultEnum.EMPTY_LOGIN_CODE);
        }
        try {
            WxMaJscode2SessionResult session = this.wxMaService.getUserService().getSessionInfo(code);
          log.info(session.getSessionKey());
           log.info(session.getOpenid());
        log.info(session.getExpiresin().toString());

            //将session写入redis用作登陆验证
            stringRedisTemplate.opsForValue().set("session", JsonUtils.toJson(session));
            return JsonUtils.toJson(session);
        } catch (WxErrorException e) {
            log.error(e.getMessage(), e);
            return e.toString();
        }
    }

    /**
     * <pre>
     * 获取用户信息接口
     * </pre>
     */
    @GetMapping("info")
    public String info(String sessionKey, String signature, String rawData, String encryptedData, String iv) {
        // 用户信息校验
        if (!this.wxMaService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
            return "user check failed";
        }
        // 解密用户信息
        WxMaUserInfo userInfo = this.wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
        Customer customer=customerService.findUserByOpenId(userInfo.getOpenId());
        if(customer==null){
            Customer newCustomer=UserInfoToCustomer.convert(userInfo);
                if(customerService.insertUser(newCustomer)){
                    log.info("新增用户啦！！！");
                    return JsonUtils.toJson(newCustomer.getBalance());
                }
            }
            return JsonUtils.toJson(customer.getBalance());

    }
}
